package com.gengealogy.net.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gengealogy.net.po.Clan;
import com.gengealogy.net.po.Member;
import com.gengealogy.net.po.User;
import com.gengealogy.net.service.ClanService;
import com.gengealogy.net.service.MemberService;
import com.gengealogy.net.service.UserService;

@Controller
public class MemberController {

	@Autowired
	private UserService userService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private ClanService clanService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	// 族谱编纂人员
	@RequestMapping(value = "/build/getMember.action")
	public String getMember(Model model, HttpSession session) {
		int member_clan_id = (int) session.getAttribute("TREE_CLAN_ID");
		List<Member> members = memberService.findMember(member_clan_id);
		if (members == null) {
			model.addAttribute("msg", "还没有家族成员哦！");
			return "build/build_member";
		} else {
			model.addAttribute("members", members);
			int user_ident = (int) session.getAttribute("USER_IDENT");
			if (user_ident == 0)
				// 跳转到普通用户主页面
				return "user/user_member";
			else
				return "build/build_member";
		}
	}

	// 用户修改家族个人信息
	@RequestMapping(value = "/user/getMyMember.action")
	public String getMyMember(Model model, HttpSession session) {
		int member_user_id = (int) session.getAttribute("USER_ID");
		Member members = memberService.findUserMember(member_user_id);
		model.addAttribute("members", members);
			return "user/user_mymember";

	}

	@RequestMapping(value = "/addMember.action", method = RequestMethod.POST)
	@ResponseBody
	public String addMember(Member member, Model model, HttpSession session) {
		int member_user_id = member.getMember_user_id();
		User user = userService.getUserById(member_user_id);
		if (user == null) {
			return "NO";
		} else {
			int rows = memberService.addMember(member);
			if (rows > 0) {
				return "OK";
			} else {
				return "FAIL";
			}
		}

	}

	@RequestMapping(value = "/joinMember.action", method = RequestMethod.POST)
	public String joinMember(Member member, Model model, HttpSession session) {
		int member_clan_id = member.getMember_clan_id();
		Clan clan = clanService.getClanById(member_clan_id);
		if (clan == null) {
			model.addAttribute("msg", "没有该家族哦！");
			return "user/gen_create";
		} else {
			session.setAttribute("TREE_CLAN_ID", member_clan_id);
			memberService.addMember(member);
			return "redirect:build/getMember.action";
		}
	}

	@RequestMapping(value = "/clan/updateMember.action", method = RequestMethod.POST)
	@ResponseBody
	public String updateMember(Member member, Model model) {
		int rows = memberService.updateMember(member);
		if (rows > 0) {
			return "OK";
		} else {
			return "FAIL";
		}

	}

	@RequestMapping(value = "/clan/deleteMember.action", method = RequestMethod.POST)
	@ResponseBody
	public String delMember(int member_id, Model model) {
		int rows = memberService.deleteMember(member_id);
		if (rows > 0) {
			return "OK";
		} else {
			return "FAIL";
		}
	}

	@RequestMapping("/getMemberById.action")
	@ResponseBody
	public Member getMemberById(int member_id, Model model, HttpSession session) {
		Member member = memberService.getMemberById(member_id);
		return member;
	}

}
